package com.tan.kotlintest.adapter

import android.content.Context
import android.support.v7.widget.RecyclerView
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.RatingBar
import android.widget.TextView
import com.tan.kotlintest.R
import com.tan.kotlintest.base.BaseActivity
import com.tan.kotlintest.bean.GirlListBean
import com.tan.kotlintest.helper.ImageLoadHelper
import com.tan.kotlintest.util.DevicesUtil
import java.util.*

/*
 * Copyright (C) 2018 重庆一聊高科技有限公司
 * 版权所有
 *
 * 功能描述:  女神recycler adapter
 * 作者：txj
 * 创建时间：2018/9/18
 *
 * 修改人：
 * 修改描述：
 * 修改日期
 */
class GirlRecyclerAdapter(private var mContext: BaseActivity?) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    private var mBeans: List<GirlListBean> = ArrayList()

    fun loadData(beans: List<GirlListBean>) {
        mBeans = beans
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val view = LayoutInflater.from(mContext).inflate(R.layout.item_girl_recycler_layout, parent, false)
        return MyViewHolder(view)
    }

    override fun getItemCount(): Int {
        return mBeans.size
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val bean: GirlListBean = mBeans[position]
        val mHolder = holder as MyViewHolder
        //封面
        val coverImg = bean.t_cover_img
        if (!TextUtils.isEmpty(coverImg)) {
            //计算图片大小
            val overWidth = DevicesUtil.getScreenW(mContext as Context) - DevicesUtil.dp2px(mContext as Context, 10F)
            val overHeight = DevicesUtil.dp2px(mContext as Context, 312F)
            ImageLoadHelper.glideShowCornerImageWithUrl(mContext, bean.t_cover_img, mHolder.mHeadIv,
                    6, overWidth, overHeight)
        }
        //昵称
        mHolder.mNameTv.text = bean.t_nickName
        //星星
        mHolder.mStarRb.rating = bean.t_score.toFloat()
        //岁数
        val age = bean.t_age
        if (age > 0) {
            val content = age.toString() + mContext?.resources?.getString(R.string.age)
            mHolder.mAgeTv.text = content
        }
    }

    inner class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        var mHeadIv: ImageView = itemView.findViewById(R.id.head_iv)
        var mNameTv: TextView = itemView.findViewById(R.id.name_tv)
        var mStarRb: RatingBar = itemView.findViewById(R.id.star_rb)
        var mAgeTv: TextView = itemView.findViewById(R.id.age_tv)
    }

}